package cn.util.Init;

import cn.controller.AopController;
import cn.util.aliyun.AliyunOSSClientUtil;
import cn.util.shiro.ShiroFilter;
import common.service.JedisClient;
import common.service.MryeDataCenterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;


import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InitDataListener implements InitializingBean,ServletContextAware {

    @Resource(name="mryeDataCenterService")
    MryeDataCenterService mryeDataCenterService;

    //OSS
    @Autowired
    AliyunOSSClientUtil aliyunOSSClientUtil;

    //Redis
    @Resource(name="jedisClient")
    JedisClient jedisClient;

    //Shiro
    @Autowired
    ShiroFilter shiroFilter;

    private Logger logger = LoggerFactory.getLogger(AopController.class);

    //初始化数据
    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String,Object> map=new HashMap<>();
        //初始化OSS对象存储
        map.put("sql","SELECT OSS_ENDPOINT,OSS_ACCESS_KEY_ID,OSS_ACCESS_SECRET,OSS_BUCKETNAME,OSS_FILEDIR FROM YST_API_OSS");
        Map<String,Object> OSS=mryeDataCenterService.searchSap(map);
        if(OSS!=null){
            try{
                aliyunOSSClientUtil.endpoint= (String) OSS.get("OSS_ENDPOINT");
                aliyunOSSClientUtil.accessKeyId=(String) OSS.get("OSS_ACCESS_KEY_ID");
                aliyunOSSClientUtil.accessKeySecret=(String) OSS.get("OSS_ACCESS_SECRET");
                aliyunOSSClientUtil.bucketName=(String) OSS.get("OSS_BUCKETNAME");
                aliyunOSSClientUtil.filedir=(String) OSS.get("OSS_FILEDIR");
                //AliyunOSSClientUtil.UploadOSS(new File("D:\\66.jpg"),1);
                logger.info("初始化阿里云OSS对象存储-----成功！");
            }catch (Exception e){
                logger.info("初始化阿里云OSS对象存储-----失败！"+e.getMessage());
            }
        }
        //初始化Redis 缓存
        map.put("sql","SELECT REDIS_ID,REDIS_HOST,REDIS_PORT,REDIS_PWD,REDIS_TYPE,REDIS_SHOW FROM YST_SYS_REDIS WHERE REDIS_SHOW=1");
        Map<String,Object> REDIS=mryeDataCenterService.searchSap(map);
        if (REDIS!=null&&REDIS.size()>0){
            try{
                jedisClient.init(REDIS);
                jedisClient.set("init","HelloRedis");
                logger.info("初始化牛逼的Redis缓存------成功！ "+jedisClient.get("init"));
            }catch (Exception e){
                logger.info("初始化牛逼的Redis缓存------失败！----------------------- "+e.getMessage());
            }

        }
        //初始化Shiro 过滤链
        map.put("sql","SELECT SHIRO_ID,SHIRO_URL,SHIRO_NAME,SHIRO_POWER,SHIRO_SHOW FROM YST_SYS_SHIRO WHERE SHIRO_SHOW=1");
        List<Map<String,Object>> shirourl=mryeDataCenterService.searchSapList(map);
        try {
            shiroFilter.ShiroInit(shirourl);
            logger.info("初始化牛逼的Shiro过滤链------成功!");
        }catch (Exception e){
            logger.info("初始化牛逼的Shiro过滤链------失败！----------------------- "+e.getMessage());
        }
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        //System.setProperty("dubbo.application.logger","slf4j");
    }
}
